/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.decea.projects.sgq.core;

import gov.decea.projects.sgq.api.MainService;
import gov.decea.projects.sgq.core.exception.CoreException;
import gov.decea.projects.sgq.dao.AerodromoDAO;
import gov.decea.projects.sgq.dao.DAOFactory;
import gov.decea.projects.sgq.dao.MeteorologicalReportGroupDataDAO;
import gov.decea.projects.sgq.exception.DAOException;
import gov.decea.projects.sgq.model.Aerodromo;
import gov.decea.projects.sgq.model.MetarGroupData;
import gov.decea.projects.sgq.model.MeteorologicalReportGroupData;
import gov.decea.projects.sgq.model.TafGroupData;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author fernandofot
 */
public class MainServiceSimpleImpl implements MainService {

    private DAOFactory factory;

    public void setFactory(DAOFactory factory) {
        this.factory = factory;
    }

    public DAOFactory getFactory() {
        return factory;
    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public Long createMeteorologicalReportGroupData(MeteorologicalReportGroupData groupData) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            Long id = dao.create(groupData);
            return id;
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }

    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void updateMeteorologicalReportGroupData(MeteorologicalReportGroupData groupData) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            dao.update(groupData);

        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }

    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void removeMeteorologicalReportGroupData(Long groupDataId) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            dao.remove(groupDataId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public MeteorologicalReportGroupData findMeteorologicalReportGroupData(Long groupDataId) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            return dao.find(groupDataId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = true)
    public List<MetarGroupData> findAllMetar(Long organizacaoId, Long aerodromoId, Date periodo) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            return dao.findAllMetar(organizacaoId, aerodromoId, periodo);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = true)
    public List<TafGroupData> findAllTaf(Long organizacaoId, Long aerodromoId, Date periodo) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            return dao.findAllTaf(organizacaoId, aerodromoId, periodo);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = true)
    public List<MetarGroupData> findAllMetar(List<Long> organizacoesIds, List<Long> aerodromosIds, List<Date> periodos) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            return dao.findAllMetar(organizacoesIds, aerodromosIds, periodos);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    @Transactional(readOnly = true)
    public List<TafGroupData> findAllTaf(List<Long> organizacoesIds, List<Long> aerodromosIds, List<Date> periodos) {
        MeteorologicalReportGroupDataDAO dao = factory.getMeteorologicalReportGroupDataDAO();

        try {
            return dao.findAllTaf(organizacoesIds, aerodromosIds, periodos);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public Long createAerodromo(Aerodromo aerodromo) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            Long id = dao.create(aerodromo);
            return id;
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void updateAerodromo(Aerodromo aerodromo) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            dao.update(aerodromo);

        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public void removeAerodromo(Long aerodromoId) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            dao.remove(aerodromoId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public Aerodromo findAerodromo(Long aerodromoId) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            return dao.find(aerodromoId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public Aerodromo findAerodromoBySigla(String sigla) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            return dao.findBySigla(sigla);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public List<Aerodromo> findAllAerodromos() {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            return dao.findAll();
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public List<Aerodromo> findAllAerodromos(Long organizacaoId) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            return dao.findAll(organizacaoId);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

    @Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"BroadCatchBlock", "TooBroadCatch"})
    public List<Aerodromo> searchAerodromos(String term) {
        AerodromoDAO dao = factory.getAerodromoDAO();
        try {
            return dao.search(term);
        } catch (DAOException ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException(ex.getMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainServiceSimpleImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new CoreException("Erro: procure o Administrador");
        }
    }

}
